<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class StorePink extends Model
{
    use HasFactory;
    protected $table='store_pink';
    protected $guarded=[];
    public $timestamps=false;
    public function GroupUser()
    {
        return $this->hasOne(User::class,'uid','uid');

    }
    public function Group()
    {
        return $this->hasOne(StoreGroupProduct::class,'id','cid');
    }
    public function user(){
        return $this->hasOne(User::class,'uid','uid')->select(['uid','real_name','avatar']);
    }

    public function storeCombination(){
        return $this->hasOne(StoreCombination::class,'id','cid')->select(['id','product_id','people']);
    }

    public static function storePinkAlter($id,$data){
        return self::where('id',$id)->update($data);
    }

    public static function storePinkWithUserWithStoreCombination($size){
        return self::with(['user','storeCombination','storeCombination.storeProduct'])->paginate($size);
    }

    public static function addStorePink($data){
        return self::insert($data);
    }

    public static function delStorePink($id){
        return self::where('id',$id)->delete();
    }
    public static function findStorePink($id){
        return self::where('id',$id)->first();
    }
    public function IsJoin($uid,$gid,$cid,$kid)
    {
        return $this->where([['k_id',$kid],['cid',$cid],['uid','=',$uid]])->first();
    }
    public function GroupCount($cid,$kid)
    {
        return $this->where([['cid','=',$cid],['k_id','=',$kid]])->get()->toArray();
    }
    public function AddPink($array)
    {
        return $this->create($array);
    }
    public function IfGroupHas($cid,$uid)
    {
        return $this->where([['cid','=',$cid],['uid','=',$uid]])->first();
    }
    public function UserInitGroup($data)
    {
        return $this->create($data);
    }
    public function FindGroup($uid,$cid,$id)
    {
        return $this->where([['uid','=',$uid],['cid','=',$cid],['id','=',$id]])->get();
    }
     public function UserExitGroup($uid,$kid,$cid)
    {
        return $this->where([['uid','=',$uid],['k_id','=',$kid],['cid','=',$cid]])->delete();
    }
    public function HeadExitGroup($uid,$cid)
    {
        return $this->where([['uid','=',$uid],['cid','=',$cid]])->delete();
    }
    public function GroupProduct()
    {
        return $this->hasOne(StoreCombination::class,'id','cid');
    }
    public function HeadUser()
    {
        return $this->hasOne(User::class,'uid','uid');
    }
}
